//
//  LQVisitorView.swift
//  Weibo
//
//  Created by ios on 16/8/8.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
class LQVisitorView: UIView {
//    撰写闭包
    var loginClosure:(()->())?
    //添加旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //设置罩层
    private lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //中心的房子
    private lazy var houseImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息信息
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人,关注一些人，回这里看看有什么惊喜关注一些人"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.numberOfLines = 0
    return label
    }()
    //注册按钮
   private lazy var registerButton:UIButton = {
    let button = UIButton()
    //添加注册点击
    button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        //添加登录点击
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI(){
        //  设置背景色
    backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
    addSubview(cycleImageView)
    addSubview(maskImageView)
    addSubview(houseImageView)
    addSubview(messageLabel)
    addSubview(registerButton)
    addSubview(loginButton)
     
    //设置约束
    cycleImageView.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
//            make.height.equalTo(35)
//            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }

    }
    //开启动画
    private func startAnimation() {
        //  创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  动画目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  执行次数
        animation.repeatCount = MAXFLOAT
        //  动画执行完成不让其释放
        animation.removedOnCompletion = false
        //  执行动画
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    
    }
    //修改访客视图信息
    func updateVisitorViewInfo(message: String?, imageName: String?){
        if let msg = message, imgName = imageName{
        messageLabel.text = msg
        houseImageView.image = UIImage(named: imgName)
        //隐藏旋转图片
        cycleImageView.hidden = true
        }else{
        cycleImageView.hidden = false
            //开启动画
            startAnimation()
        }
    }
    
     @objc private func registerButtonAction(){
      print("注册")
        //执行闭包
        loginClosure?()
    
    }
    @objc private func loginButtonAction(){
        print("登录")
        //执行闭包
        loginClosure?()
    }
    
}
